package com.xca.okhttp.views;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.xca.okhttp.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private CardView m1 , m2 , m3 , m4 , m5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }


    private void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.tf , fragment);
        transaction.commit() ;
    }

    private void startAnimation1() {
        // 创建三个不同的ObjectAnimator实例
        ObjectAnimator fadeIn = ObjectAnimator.ofFloat(m1, "alpha", 0f, 1f);
        //ObjectAnimator grow = ObjectAnimator.ofFloat(m1, "scaleX", 0.2f, 1f);
        ObjectAnimator move = ObjectAnimator.ofFloat(m1, "translationX", 0f, -210f);

        // 对于向上移动，可以添加另一个ObjectAnimator针对translationY属性
        ObjectAnimator moveUp = ObjectAnimator.ofFloat(m1, "translationY", 0f, -120f);

        // 使用AnimatorSet来组合这些动画
        AnimatorSet set = new AnimatorSet();
        set.playTogether(fadeIn, move, moveUp);
        set.setDuration(300); // 设置动画持续时间为1秒
        set.start(); // 启动动画
    }

    private void startAnimation11() {
        // 创建三个不同的ObjectAnimator实例
        ObjectAnimator fadeIn = ObjectAnimator.ofFloat(m1, "alpha", 1f, 0f);
        //ObjectAnimator grow = ObjectAnimator.ofFloat(m1, "scaleX", 1f, 1.5f);
        ObjectAnimator move = ObjectAnimator.ofFloat(m1, "translationX", -210f, 0f);

        // 对于向上移动，可以添加另一个ObjectAnimator针对translationY属性
        ObjectAnimator moveUp = ObjectAnimator.ofFloat(m1, "translationY", -120f, 0f);

        // 使用AnimatorSet来组合这些动画
        AnimatorSet set = new AnimatorSet();
        set.playTogether(fadeIn,  move, moveUp);
        set.setDuration(300); // 设置动画持续时间为1秒
        set.start(); // 启动动画
    }

    private void startAnimation2() {
        // 创建三个不同的ObjectAnimator实例
        ObjectAnimator fadeIn = ObjectAnimator.ofFloat(m2, "alpha", 0f, 1f);
        // ObjectAnimator grow = ObjectAnimator.ofFloat(m2, "scaleX", 1f, 1.5f);
        //  ObjectAnimator move = ObjectAnimator.ofFloat(m2, "translationX", 0f, -200f);

        // 对于向上移动，可以添加另一个ObjectAnimator针对translationY属性
        ObjectAnimator moveUp = ObjectAnimator.ofFloat(m2, "translationY", 0f, -200f);

        // 使用AnimatorSet来组合这些动画
        AnimatorSet set = new AnimatorSet();
        set.playTogether(fadeIn,  moveUp);
        set.setDuration(300); // 设置动画持续时间为1秒
        set.start(); // 启动动画
    }

    private void startAnimation22() {
        // 创建三个不同的ObjectAnimator实例
        ObjectAnimator fadeIn = ObjectAnimator.ofFloat(m2, "alpha", 1f, 0f);
        // ObjectAnimator grow = ObjectAnimator.ofFloat(m2, "scaleX", 1f, 1.5f);
        //  ObjectAnimator move = ObjectAnimator.ofFloat(m2, "translationX", 0f, -200f);

        // 对于向上移动，可以添加另一个ObjectAnimator针对translationY属性
        ObjectAnimator moveUp = ObjectAnimator.ofFloat(m2, "translationY", -200f, 0f);

        // 使用AnimatorSet来组合这些动画
        AnimatorSet set = new AnimatorSet();
        set.playTogether(fadeIn,  moveUp);
        set.setDuration(300); // 设置动画持续时间为1秒
        set.start(); // 启动动画
    }

    private void startAnimation3() {
        // 创建三个不同的ObjectAnimator实例
        ObjectAnimator fadeIn = ObjectAnimator.ofFloat(m3, "alpha", 0f, 1f);
        //ObjectAnimator grow = ObjectAnimator.ofFloat(m3, "scaleX", 1f, 1.5f);
        ObjectAnimator move = ObjectAnimator.ofFloat(m3, "translationX", 0f, 210f);

        // 对于向上移动，可以添加另一个ObjectAnimator针对translationY属性
        ObjectAnimator moveUp = ObjectAnimator.ofFloat(m3, "translationY", 0f, -120f);

        // 使用AnimatorSet来组合这些动画
        AnimatorSet set = new AnimatorSet();
        set.playTogether(fadeIn, move, moveUp);
        set.setDuration(300); // 设置动画持续时间为1秒
        set.start(); // 启动动画
    }

    private void startAnimation33() {
        // 创建三个不同的ObjectAnimator实例
        ObjectAnimator fadeIn = ObjectAnimator.ofFloat(m3, "alpha", 1f, 0f);
        //ObjectAnimator grow = ObjectAnimator.ofFloat(m3, "scaleX", 1f, 1.5f);
        ObjectAnimator move = ObjectAnimator.ofFloat(m3, "translationX", 210f, 0f);

        // 对于向上移动，可以添加另一个ObjectAnimator针对translationY属性
        ObjectAnimator moveUp = ObjectAnimator.ofFloat(m3, "translationY", -120f, 0f);

        // 使用AnimatorSet来组合这些动画
        AnimatorSet set = new AnimatorSet();
        set.playTogether(fadeIn, move, moveUp);
        set.setDuration(300); // 设置动画持续时间为1秒
        set.start(); // 启动动画
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.b1){
            replaceFragment(new HotSearchFragment());
        } else if (v.getId() == R.id.b2) {
            replaceFragment(new DictionaryFragment());
        } else if (v.getId()==R.id.b3) {
            replaceFragment(new TranslateFragment());
        } else if (v.getId() == R.id.b4) {
            m4.setVisibility(View.GONE);
            m5.setVisibility(View.VISIBLE);
            startAnimation1();
            startAnimation2();
            startAnimation3();
        } else if (v.getId() == R.id.b5) {
            m4.setVisibility(View.VISIBLE);
            m5.setVisibility(View.GONE);
            startAnimation11();
            startAnimation22();
            startAnimation33();
        }
    }
    private void init(){
        m1 = findViewById(R.id.b1);
        m2 = findViewById(R.id.b2);
        m3 = findViewById(R.id.b3);
        m4 = findViewById(R.id.b4);
        m5 = findViewById(R.id.b5);
        m5.setVisibility(View.GONE);
        replaceFragment(new HotSearchFragment());
        m1.setOnClickListener(this);
        m2.setOnClickListener(this);
        m3.setOnClickListener(this);
        m4.setOnClickListener(this);
        m5.setOnClickListener(this);
    }
}